package com.mxx.qh.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;

import com.mxx.qh.R;
import com.mxx.qh.interfaces.IException;
import com.mxx.qh.interfaces.IGlobalException;
import com.mxx.qh.interfaces.ILoading;
import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzoneShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: WxShareUtils
 * @Description: java类作用描述
 * @Author: ljf89 (刘江飞)
 * @Date: 2021/1/11-- 15:59
 * @Email: 642371742@qq.com
 */
public class QQShareUtils {
    private static final int THUMB_SIZE = 150;

    /**
     * 分享网页类型至微信
     *
     * @param context 上下文
     * @param appId   微信的appId
     * @param webUrl  网页的url
     * @param title   网页标题
     * @param content 网页描述
     * @param url     位图
     */
    public static void share(Activity context, String appId
            , int type, String title, String content, String webUrl, String url
            , ILoading iLoading, IException iException) {
        if (!isQQClientAvailable(context)) {
            iLoading.onFinish();
            iException.handlerException("您尚未安装QQ");
            return;
        }
        try {
            Tencent mTencent = Tencent.createInstance(appId, context, "com.mxx.qh.fileprovider");
            Bundle params = new Bundle();
            IUiListener uiListener = new IUiListener() {
                @Override
                public void onComplete(Object o) {
                    iLoading.onFinish();
                }

                @Override
                public void onError(UiError uiError) {
                    iException.handlerException(uiError.errorCode + "\n" + uiError.errorMessage);
                }

                @Override
                public void onCancel() {
                    iException.handlerException("取消分享");
                }

                @Override
                public void onWarning(int i) {
                    iException.handlerException("Warning" + "\n" + i);
                }
            };
            if (type == 3)//分享QQ好友
            {
                params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, getQQShareType(type));//分享的类型
                params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, webUrl);//内容地址
                params.putString(QQShare.SHARE_TO_QQ_TITLE, title);//分享标题
                params.putString(QQShare.SHARE_TO_QQ_SUMMARY, content);//要分享的内容摘要
                params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, url);//分享的图片URL
                params.putString(QQShare.SHARE_TO_QQ_APP_NAME, context.getResources().getString(R.string.app_name));//应用名称
                iLoading.onFinish();
                mTencent.shareToQQ(context, params, uiListener);
            }
            if (type == 4)//分享QQ空间
            {
                params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, getQQShareType(type));
                params.putString(QzoneShare.SHARE_TO_QQ_TITLE, title);//必填
                params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, content);//选填
                params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, webUrl);//必填
                ArrayList<String> list = new ArrayList<>();
                list.add(url);
                params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, list);
                iLoading.onFinish();
                mTencent.shareToQzone(context, params, uiListener);
            }

        } catch (Exception e) {
            e.printStackTrace();
            iLoading.onFinish();
            iException.handlerException(e.getMessage());
        }

    }

    private static int getQQShareType(int type) {
        if (type == 3)//分享QQ好友
        {
            return QQShare.SHARE_TO_QQ_TYPE_DEFAULT;
        }
        if (type == 4)//分享QQ空间
        {
            return QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT;
        }
        return QQShare.SHARE_TO_QQ_TYPE_DEFAULT;
    }

    /**
     * 判断qq是否可用
     *
     * @param context
     * @return
     */
    public static boolean isQQClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mobileqq")) {
                    return true;
                }
            }
        }
        return false;
    }
}
